import 'package:flutter/material.dart';
import 'package:proj_sfv/component/LoadingOverlay.dart';
import 'package:proj_sfv/component/MyListItem.dart';
import 'package:proj_sfv/component/MyNotification.dart';
import 'package:proj_sfv/domain/UserInfo.dart';
import 'package:proj_sfv/http/requests.dart';
import 'package:proj_sfv/store/MyArguments.dart';
import 'package:http/http.dart' as http;
import 'package:tencent_cloud_chat_uikit/tencent_cloud_chat_uikit.dart';

class UserInfoPage extends StatefulWidget {
  @override
  State<UserInfoPage> createState() => _UserInfoPageState();
}

class _UserInfoPageState extends State<UserInfoPage> {
  Future<UserInfo> _infoFuture;
  UserInfo _userInfo;
  bool _loading = true;
  MyArguments _args;

  @override
  void didChangeDependencies() {
    _args = ModalRoute.of(context).settings.arguments as MyArguments;
    initData();
  }

  initData() {
    getBasicMyRequest(context, _args.id, _args.token).then((value) {
      setState(() {
        _userInfo = value;
        _loading = false;
      });
    });
  }

  @override
  void initState() {
    _userInfo = UserInfo.empty();
    super.initState();
  }

  void tryUpdate(String value, String fieldName) async {
    setState(() {
      _loading = true;
    });
    await modify(value, fieldName);
    initData();
  }

  void modify(String value, String fieldName) async {
    String text = await showDialog<String>(context: context, builder: (BuildContext context) => ModifyDialog(value, fieldName));
    if (text != null) {
      print('try update: ${_args.id}, ${_args.token}, text $text filedName $fieldName');
      if (updateUserInfo(context, _args.id, _args.token, fieldName, text) != null) {
        ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text("更新成功")));
        setState(() {
          _userInfo.setFieldValue(fieldName, text);
        });
      }
      if (fieldName == "userName") {
        await TencentImSDKPlugin.v2TIMManager.setSelfInfo(userFullInfo: V2TimUserFullInfo(nickName: text));
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("用户信息"),
      ),
      body: LoadingOverlay(
        isLoading: _loading,
        child: FutureBuilder(
          builder: (context, snapshot) =>
            ListView(
              children: [
                MyListItem.of("id", _userInfo.id.toString()),
                MyListItem.of("昵称", _userInfo.userName, onTap: () => tryUpdate(_userInfo.userName, "userName")),
                MyListItem.of("签名", _userInfo.signature, onTap: () => tryUpdate(_userInfo.signature, "signature")),
                MyListItem.of("手机号", _userInfo.phoneNumber, onTap: () => tryUpdate(_userInfo.phoneNumber, "phoneNumber"),),
                MyListItem.of("性别", _userInfo.gender ? "男" : "女"),
                MyListItem.of("邮箱", _userInfo.email, onTap: ()=> tryUpdate(_userInfo.email, "email"),),
                MyListItem.of("qq号", _userInfo.qqNumber, onTap: () => tryUpdate(_userInfo.qqNumber, "qqNumber"),),
                MyListItem.of("微信号", _userInfo.vxNumber, onTap: ()=> tryUpdate(_userInfo.vxNumber, "vxNumber"),),
                MyListItem.of("生日", _userInfo.birthday == null ? "" : _userInfo.birthday.toIso8601String()),
                MyListItem.of("地址", _userInfo.currentAddress),
                MyListItem.of("家乡", _userInfo.hometown),
                MyListItem.of("毕业学校", _userInfo.graduatedSchool),
              ],
            )
        ),
      ),
    );
  }
}